﻿using System;
using System.Collections.Generic;
using SAPbouiCOM;
using System.Reflection;
using System.Text.RegularExpressions;

namespace BoAddonBiz_FreightForwarder
{
    /// <summary>
    /// 合同管理窗体
    /// </summary>
    /// <author>Webb.Wen</author>
    /// <lastedit>2011-07-20 13:44:19</lastedit>
    class B1Form_Contract : SAPBoAddon.B1Form
    {
        string FORM_NOTNULLABLE;

        public B1Form_Contract()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_Contract_frm_ItemEvent);
        }

        void B1Form_Contract_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            { }
            else
            {
                //#region 手动模拟
                //List<RegexValidationConfig> list = new List<RegexValidationConfig>();
                //RegexValidationConfig U_SerialNum = new RegexValidationConfig();
                //U_SerialNum.PropertyName = "U_SerialNum";
                //U_SerialNum.ValidationRegex = @"HX\d{10}";//假设是HX+10位数字
                //list.Add(U_SerialNum);
                ////ValidationConfig U_CardCode = new ValidationConfig();
                ////U_CardCode.PropertyName = "U_CardCode";
                ////U_CardCode.ValidationRegex = @"\d+";//不能为空的数字
                ////list.Add(U_CardCode);
                //list.Add(new RegexValidationConfig { PropertyName = "U_CardCode", ValidationRegex = @"\d+" });
                //RegexValidationConfigCollection configs = new RegexValidationConfigCollection(list);
                //#endregion

                if (pVal.EventType == BoEventTypes.et_GOT_FOCUS || pVal.EventType == BoEventTypes.et_LOST_FOCUS)
                {
                    EditText edt = this.CurrentForm.Items.Item(pVal.ItemUID).Specific as EditText;
                    if (edt != null)
                    {
                        //RegexValidationResult result = this.ValidateEditText(edt, configs);
                        //if (result.Success)
                        //{
                        //    edt.BackColor = -1;//-1就是常规颜色
                        //}
                        //else
                        //{
                        //    edt.BackColor = -18751 ;
                        //}
                    }
                }
            }
        }

        protected override bool BeforeSaveCheckFormData()
        {
            KeyValuePair<string, string> uid_description;
            bool check = Utils.CheckFormNullable(this.CurrentForm, FORM_NOTNULLABLE, out uid_description);
            if (!check)
            {
                SBOApp.SetStatusBarMessage(String.Format(B1ResourceManager.GetInstance().GetMessageFormat(this, SBOApp.Language, "CanNotBeNull"), uid_description.Value), SAPbouiCOM.BoMessageTime.bmt_Short, true);
                this.CurrentForm.Items.Item(uid_description.Key).Click(BoCellClickType.ct_Regular);
            }
            return check;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {

        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_FF_Contract;
            FormSetting.FormFileName = B1FormTypes.ado_AVA_FF_Contract.ToString() + "Form.srf";
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            FORM_NOTNULLABLE = B1ResourceManager.GetInstance().GetConfig(this, "FORM_NOTNULLABLE");
        }


        //public RegexValidationResult ValidateEditText(EditText edt, RegexValidationConfigCollection configs)
        //{
        //    if (edt.DataBind.DataBound && edt.DataBind.Alias.Length > 0)
        //    {
        //        RegexValidationConfig config = configs.List.FirstOrDefault(p => p.PropertyName == edt.DataBind.Alias);
        //        if (config != null)
        //        {
        //            if (!String.IsNullOrEmpty(config.InvokeMember))
        //            {
        //                int index = new Regex(@"\.", RegexOptions.RightToLeft).Match(config.InvokeMember).Index;

        //                string assembly = config.InvokeMember.Substring(0, index);
        //                string function = config.InvokeMember.Substring(index + 1);
        //                object obj = Assembly.GetExecutingAssembly().CreateInstance(assembly, true);
        //                Type type = Assembly.GetExecutingAssembly().GetType(assembly);
        //                if (!(bool)type.InvokeMember(function,
        //                      BindingFlags.DeclaredOnly |
        //                      BindingFlags.Public | BindingFlags.NonPublic |
        //                      BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, null))
        //                {
        //                    RegexValidationResult result = new RegexValidationResult(false);
        //                    result.Config = config;
        //                    result.InvalidProperty = "";// p.Name;
        //                    result.PropertyValue = null;
        //                    return result;
        //                }
        //            }
        //            else
        //            {
        //                Regex regex = new Regex(config.ValidationRegex);
        //                object value = edt.Value;
        //                if (!regex.IsMatch(value.ToString()))
        //                {
        //                    RegexValidationResult result = new RegexValidationResult(false);
        //                    result.Config = config;
        //                    result.InvalidProperty = edt.DataBind.Alias;
        //                    result.PropertyValue = value;
        //                    return result;
        //                }
        //            }
        //        }
        //    }
        //    return new RegexValidationResult(true);
        //}
    }


}
